<?php
namespace XWAM\Addon\PXCallSystem\Controller;
/**
 * 会议室
 *
 * @author Alen
 * @since 1.0 2014-9-24 by sutroon
 */
class MeetingRoomController extends AppbaseController {

    public function index(){
        $this->display();
    }
    public function get_meetingroom_search_data(){
        $search=I();
        if (!$this->isSuperAdmin) {
            $where['siteID'] = $this->loginData['siteID'];
        }
    }
    public function get_meetingroom_list_json() {
        $search_data=$this->get_meetingroom_search_data();
        $list = AddonD('MeetingRoom')->get_meetingroom_list($search_data['where']);
        $this->ajaxReturn($list);
    }
    public function meetingroom_export(){
        $list = AddonD('MeetingRoom')->scope('export')->select();
        if (!$list) {
            $this->ajaxMsg(false,'找不到符合条件的记录!');
        }
        sofn_excel_export($list);        
    }

    public function item_edit($id=0) {
        if ($id) {
            $this->assign('data', M('tcti_meeting_room')->find($id));
        }
        $this->display();
    }

    public function item_edit_save() {
        $params = new \Org\Su\Params();
        $params->addParam('siteID', 'require', '租户未选择!');
        $params->addParam('exten', 'require', '未填写会议号!');
        $params->addParam('description', 'require', '未填写名称!');
        if ($params->hasError()) {
            $this->ajaxMsg(false, $params->getErrors());
        }
        $id = I('id');
        $exten = I('exten');
        $msg = StatusController::cti_check_extension_func($exten);
        if (!$id && true !== $msg) {
            $this->ajaxMsg(false, $msg);
        }

        $params->addData('userpin', '');
        $params->addData('adminpin', '');
        $params->addData('options', '');
        $params->addData('joinmsg_id', '0');
        $params->addData('music', 'inherit');
        $params->addData('users', '0');
        $M = M('tcti_meeting_room');
        if ($id) {
            // $exten=$M->where("id=$id")->getField('exten');
            $result = $M->where("id=$id")->save($params->getParams());
        } else {
            $result = $M->add($params->getParams());
            $filename = '/etc/asterisk/extensions_ecall.conf';
            $content = file_get_contents($filename);
            $content.=PHP_EOL . "exten => $exten,1,MeetMe($exten,Mdqx)" . PHP_EOL . "exten => $exten,n,Hangup()";
            file_put_contents($filename, $content);
        }
        if ($result !== false) {
            if ($id) {
                $result = $M->execute("DELETE FROM asterisk.meetme WHERE exten = '" . $exten . "'");
            }
            $result = $M->execute($params->get_insert_sql('asterisk.meetme', array('siteID')));
        }
        if ($result === false) {
            $this->ajaxMsg(false, '保存失败!');
        } else {
            $this->force_need_reload(false);
            $this->ajaxMsg(true,'保存成功', AddonU('MeetingRoom/index'));
        }
    }

    public function item_delete($id='') {
        if (!$id) {
            $this->ajaxMsg(false, '~参数有误');
        }
        $M = M('tcti_meeting_room');
        $account = $M->where("id = $id")->getField('exten');
        $result = $M->execute("DELETE FROM asterisk.meetme WHERE exten = '$account'");
        if ($result !== false) {
            $result = $M->delete($id);
        }
        if ($result === false) {
            $this->ajaxMsg(false, '~删除失败!');
        } else {
            $filename = '/etc/asterisk/extensions_ecall.conf';
            $content = file_get_contents($filename);
            $content = str_replace("exten => $account,1,MeetMe($account,Mdqx)", '', $content);
            $content = str_replace("exten => $account,n,Hangup()", '', $content);
            file_put_contents($filename, $content);
            $this->force_need_reload(false);
            $this->success('删除成功', AddonU('MeetingRoom/index'));
            //$this->act_ajaxReturn(true, '删除成功!');
            // $this->ajax_cti_reload(AddonU('MeetingRoom/index'));
        }
    }

}
